using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Shuriken.Core.Utils;
using Shuriken.Core; 
using Shuriken.Core.Store;

namespace Shuriken
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmHidden : System.Windows.Forms.Form
	{
		private System.Windows.Forms.NotifyIcon notifyIcon1;
		private System.ComponentModel.IContainer components;

		private static frmHidden m_ourselves = null;
		private System.Windows.Forms.ContextMenu contextMenu1;
        private System.Windows.Forms.MenuItem menuItem1;
        private MenuItem menuItem2;
        private MenuItem menuItem4;
        private MenuItem menuItem5;

		const int WM_HOTKEY = 0x312;

		public frmHidden()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();            
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmHidden));
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenu1 = new System.Windows.Forms.ContextMenu();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // notifyIcon1
            // 
            this.notifyIcon1.ContextMenu = this.contextMenu1;
            this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
            this.notifyIcon1.Text = "Shuriken";
            this.notifyIcon1.Visible = true;
            this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
            // 
            // contextMenu1
            // 
            this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem5,
            this.menuItem4,
            this.menuItem2,
            this.menuItem1});
            // 
            // menuItem5
            // 
            this.menuItem5.Index = 0;
            this.menuItem5.Text = "About";
            this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
            // 
            // menuItem4
            // 
            this.menuItem4.Index = 1;
            this.menuItem4.Text = "Settings";
            this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 2;
            this.menuItem2.Text = "-";
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 3;
            this.menuItem1.Text = "E&xit";
            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            // 
            // frmHidden
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(376, 112);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmHidden";
            this.Opacity = 0;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Load += new System.EventHandler(this.frmHidden_Load);
            this.Closed += new System.EventHandler(this.frmHidden_Closed);
            this.ResumeLayout(false);

		}
		#endregion

		public static frmHidden GetInstance()
		{
			if(frmHidden.m_ourselves == null)
			{
				frmHidden.m_ourselves = new frmHidden();
			}

			return m_ourselves; 
		}

		private void frmHidden_Closed(object sender, System.EventArgs e)
		{
			Hotkey.UnregisterFormHotkey();
			frmHidden.m_ourselves = null;
		}

        /// <summary>
        /// Custom Window Procedure
        /// </summary>
        /// <param name="m">Message from Windows</param>
        /// <remarks>
        /// Custom window procedure to handle special messages for the application.
        /// Right now the only special message is hte hotkey message. 
        /// </remarks>
		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
                case WM_HOTKEY:
                    HandleWmHotKey(ref m);
                    break;

				default:
					base.WndProc(ref m);
					break;
			}
		}

		private void HandleWmHotKey(ref Message m)
		{
            System.Diagnostics.Debug.WriteLine("HandleWmHotKey (frmHidden.cs)", "UI");

			int idCmd = (int)m.WParam;
			uint fsModifiers = ((uint)m.LParam & 0xFFFF);
			uint vk = ((uint)m.LParam >> 16);

			switch (idCmd)
			{
				case 1:
                    Core.UI.UIManager.GetDefaultInstance().ShowHideAction(Shuriken.Core.UI.ShowHideReason.Hotkey);
					break;
			}
		}

        /// <summary>
        /// Public method for exposing form load handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void LoadForm(object sender, System.EventArgs e)
        {
            frmHidden_Load(sender, e);        
        }

		private void frmHidden_Load(object sender, System.EventArgs e)
		{
            // Initialize the store manager 
            DateTime storestart = DateTime.Now; 
            StoreManager.GetDefaultInstance();
            DateTime storeend = DateTime.Now;

            TimeSpan ts = storeend.Subtract(storestart);
            Tracer.WriteVerbose("StoreManager initialized in " + ts.TotalSeconds+ " seconds.");

            // Register hotkey for application 
            Hotkey.RegisterHotkeyAtStartup(); 

            // Instantiate UIManager
            Core.UI.UIManager.GetDefaultInstance();
		}

		private void notifyIcon1_Click(object sender, System.EventArgs e)
		{
            MouseEventArgs m = (MouseEventArgs)e;

            Tracer.WriteVerbose("notifyIcon1_Click (frmHidden.cs)", "UI");
            if (m.Button == MouseButtons.Left)
            {
                Core.UI.UIManager.GetDefaultInstance().ShowHideAction(Shuriken.Core.UI.ShowHideReason.NotifyIcon);
            }
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
			QuitApplication();
		}

		private void QuitApplication()
		{
			notifyIcon1.Visible = false; 

			Close();
			Dispose();
			System.Diagnostics.Process.GetCurrentProcess().Kill();
		}

        private void menuItem3_Click(object sender, EventArgs e)
        {            
            //  Check for application update.
            if (Utils.Deployment.CheckApplicationUpdate())
            {
                if (MessageBox.Show("New version available. Would you like to update your Shuriken installation?", "Checking for application update", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    Utils.Deployment.DownloadUpdateAsync();                    
                    Application.Restart();
                }
            }
            else
            {
                MessageBox.Show("No new version available!", "Checking for application update",MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            // Settings invoked 
            frmSettings settings = new frmSettings();
            settings.ShowDialog();
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.ShowDialog();
        }
	}
}
